﻿using LD.BC;
using LD.Host;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 右下角弹窗.DiagShow;

namespace LD.Tool
{
    public partial class TooLaserTestFrm : FrmToolBaseSkip
    {
        public TooLaserTestFrm()
        {
            InitializeComponent();
            this.Shown += ToolMoveFrm_Shown;
            this.btn_ok.Click += Btn_ok_Click;
        }

        private void Btn_ok_Click(object sender, EventArgs e)
        {
            if (this.cob_out.SelectedIndex >= 0)
            {
                Dialog dialog = new Dialog();
                if (string.IsNullOrEmpty(cob_out.Text))
                {
                    dialog.Warning("触发信号未选择");
                    return;
                }
                if (string.IsNullOrEmpty(cob_inOk.Text))
                {
                    dialog.Warning("OK信号未选择");
                    return;
                }
                if (string.IsNullOrEmpty(cob_inNg.Text))
                {
                    dialog.Warning("NG信号未选择");
                    return;
                }
                if (string.IsNullOrEmpty(txt_testQty.Text))
                {
                    dialog.Warning("点位序号不能为空");
                    return;
                }

                ToolLaserParam ioset = new ToolLaserParam()
                {
                    OutName = this.cob_out.Text,
                    OkName = this.cob_inOk.Text,
                    NgName = this.cob_inNg.Text,
                    DelayTime = !string.IsNullOrEmpty(txt_delay.Text) ? Convert.ToInt32(txt_delay.Text) : 0,
                    TestNo = !string.IsNullOrEmpty(txt_testQty.Text) ? Convert.ToInt32(txt_testQty.Text) : 0,
                    TestEnd = chk_testEnd.Checked,
                };
                this.TB_Tool.Param = Newtonsoft.Json.JsonConvert.SerializeObject(ioset);
            }
        }

        private void ToolMoveFrm_Shown(object sender, EventArgs e)
        {
            Init();
        }
        private void Init()
        {
            //var outList = BcFactory.Instance.Config.IoList.Where(p => p.IoType == (int)IoType.输出).ToList();
            foreach (var item in ETHModbus.GetOut)
            {
                this.cob_out.Items.Add(item.Name);
            }
            //var inList = BcFactory.Instance.Config.IoList.Where(p => p.IoType == (int)IoType.输入).ToList();
            foreach (var item in ETHModbus.GetIn)
            {
                this.cob_inOk.Items.Add(item.Name);
                this.cob_inNg.Items.Add(item.Name);
            }
            if (this.TB_Tool.Param == null)
                return;


            var tool = Newtonsoft.Json.JsonConvert.DeserializeObject<ToolLaserParam>(this.TB_Tool.Param);
            if (tool != null)
            {
                this.cob_out.Text = tool.OutName;
                this.cob_inOk.Text = tool.OkName;
                this.cob_inNg.Text = tool.NgName;
                txt_delay.Text = tool.DelayTime.ToString();
                txt_testQty.Text = tool.TestNo.ToString();
                chk_testEnd.Checked = tool.TestEnd;
            }
        }
    }
}
